package org.geekbang.projects.config.source;

import org.apache.commons.collections4.MapUtils;
import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* 抽象的配置源
* @author : lsq
*/
public  abstract class AbstractConfigSource implements ConfigSource {


	private int ordinal;


	private String name;

	public AbstractConfigSource(int ordinal ,String name ){
		this.ordinal = ordinal;
		this.name = name;
	}

	@Override
	public Set<String> getPropertyNames() {
		return getProperties().keySet();
	}

	@Override
	public String getValue(String propertyName) {
		return getProperties().get(propertyName);
	}

	@Override
	public String getName() {
		return this.name;
	}

	public Map<String, String> getProperties() {
		try {
			Map<String, String> source = this.getSource();
			return MapUtils.isEmpty(source) ? new HashMap<>() : source;
		} catch (Throwable throwable) {
			throw new IllegalStateException(throwable);
		}
	}


	public int getOrdinal() {
		return this.ordinal;
	}


	protected abstract  Map<String , String> getSource() throws Throwable;
}
